<?php
/**
 * Created by PhpStorm.
 * User: spatra
 * Date: 14-11-24
 * Time: 下午8:03
 */

class LessonDiscussionReplyController extends \BaseController
{

    /**
     * 返回某一个课程讨论的恢复列表.
     *
     * @param $lessonId
     * @param $discussionId
     * @return \Illuminate\Http\JsonResponse
     *
     * 返回的JSON格式如下：
     * [
     *  {
     *    "id": "主键id",
     *    "content": "回复的内容",
     *    "created_at": "创建的时间",
     *    "creater_id": "回复人的id",
     *    "creater_name": "回复人的用户名"
     *   },
     *   //.....注意这是个数组
     * ]
     */
    public function index($lessonId, $discussionId)
    {
        $this->checkRelation($lessonId, $discussionId);

        $replies = Lesson_Discussion_Reply::getDiscussionReplies($lessonId);

        return Response::json($replies);
    }


    /**
     * 根据某一恢复id，查询某一个课程讨论的具体信息.
     *
     * @param $lessonId
     * @param $discussionId
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * 返回的JSON格式如下:
     *  {
     *    "id": "主键id",
     *    "content": "回复的内容",
     *    "created_at": "创建的时间",
     *    "creater_id": "回复人的id",
     *    "create": {
     *      "id": "回复人的id",
     *      "username": "回复人的用户名",
     *      "email": "回复人的电子邮件",
     *      "description": "回复人的简介",
     *   }
     * }
     */
    public function show($lessonId, $discussionId, $id)
    {
        $this->checkRelation($lessonId, $discussionId);

        $targetReply = Lesson_Discussion_Reply::where('discussion_id', $discussionId)
            ->where('id', $id)
            ->firstOrFail();

        $data = $targetReply->toArray();
        $data['creater'] = $targetReply['creater']->toArray();

        return Response::json($data);
    }

    /**
     * 新建回复, 请求的数据格式如下：
     *  content: 回复的内容
     *
     * @param $lessonId
     * @param $discussionId
     * @return \Illuminate\Http\JsonResponse
     *
     * 如果创建成功，则返回：
     * {
     *   "id": "新创建的回复所对应的id"
     * }
     * 如果出错，则返回：
     * {
     *   "error": "相关的错误信息"
     * }
     */
    public function store($lessonId, $discussionId)
    {
        $this->checkRelation($lessonId, $discussionId);

        $newReply = Lesson_Discussion_Reply::create( $this->buildStoreData($discussionId) );

        return Response::json([
           'id'=>$newReply['id']
        ]);
    }

    /**
     * 类内部使用，用于创建新建回复的数据.
     *
     * @param $discussionId
     * @return mixed
     */
    protected function buildStoreData($discussionId)
    {
        $addData['content'] = Purifier::clean( Input::get('content') );
        $addData['creater_id'] = Auth::user()['id'];
        $addData['discussion_id'] = $discussionId;

        return $addData;
    }

    /**
     * 暂时未决定是否启用
     *
     * @param $lessonId
     * @param $discussionId
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($lessonId, $discussionId, $id)
    {
        return Response::make('Forbidden', 403);
    }


    /**
     * 内部使用，用于检查课程和课程讨论是否存在匹配关系.
     *
     * @param $lessonId
     * @param $discussionId
     */
    protected function checkRelation($lessonId, $discussionId)
    {
        Lesson_Discussion::findDiscussionOnLessonOrFail($lessonId, $discussionId);
    }
}